//
//  XNAllotViewController.m
//  XINNingOA
//
//  Created by samuel on 2021/7/16.
//

#import "XNAllotViewController.h"
#import "XNAllotBillInfoModel.h"
#import "XNAllotApproveRequest.h"
#import "XNAllotBillInfoRequest.h"

@interface XNAllotViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) XNAllotBillInfoModel *infoModel;

@end

@implementation XNAllotViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    [SVProgressHUD show];
    __weak typeof(self)weakSelf = self;
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_group_t group = dispatch_group_create();
    dispatch_group_enter(group);
    XNAllotBillInfoRequest *infoRequest = [[XNAllotBillInfoRequest alloc] initWithOrderId:self.orderId];
    [infoRequest startWithCompletionBlockWithSuccess:^(__kindof XNAllotBillInfoRequest * _Nonnull request) {
        weakSelf.infoModel = request.infoModel;
        dispatch_group_leave(group);
    } failure:^(__kindof XNAllotBillInfoRequest * _Nonnull request) {
        dispatch_group_leave(group);
    }];
    
    dispatch_group_enter(group);
    XNAllotApproveRequest *approveRequest = [[XNAllotApproveRequest alloc] initWithOrderId:self.orderId];
    [approveRequest startWithCompletionBlockWithSuccess:^(__kindof XNAllotApproveRequest * _Nonnull request) {
        weakSelf.approveArray = request.approveFlowArray;
        dispatch_group_leave(group);
    } failure:^(__kindof XNAllotApproveRequest * _Nonnull request) {
        dispatch_group_leave(group);
    }];
    
    dispatch_group_notify(group, queue, ^{
        [weakSelf creatTableData];
        dispatch_async(dispatch_get_main_queue(), ^{
            [SVProgressHUD dismiss];
            [weakSelf.tableView reloadData];
        });

    });
    
}



- (void)creatTableData
{
    XNTableShowDataModel *infoModel = [[XNTableShowDataModel alloc] init];
    infoModel.showType = XNShowType_Normal;
    infoModel.title = @"单据信息";
    XNShowItemModel *itemModel = [[XNShowItemModel alloc] init];
    NSArray *showItemArray = [NSArray arrayWithObjects:
                              [NSString stringWithFormat:@"单据编号： %@",self.infoModel.ID],
                              [NSString stringWithFormat:@"制单日期： %@",self.infoModel.createTimeStr],
                              [NSString stringWithFormat:@"制单人： %@",self.infoModel.createUserName],
                              nil];
    itemModel.showItemArray = showItemArray;
    [infoModel.showDataArray addObject:itemModel];
    [self.showArray addObject:infoModel];
    
    XNTableShowDataModel *submitApplyModel = [[XNTableShowDataModel alloc] init];
    submitApplyModel.showType = XNShowType_Normal;
    submitApplyModel.title = @"调拨信息";
    XNShowItemModel *itemModel2 = [[XNShowItemModel alloc] init];
    NSArray *showItemArray2 = [NSArray arrayWithObjects:
                               [NSString stringWithFormat:@"资产编号: %@",self.infoModel.asset.assetNumber],
                               [NSString stringWithFormat:@"资产名称： %@",self.infoModel.asset.assetName],
                               [NSString stringWithFormat:@"规格型号： %@",self.infoModel.asset.modelNumber],
                               [NSString stringWithFormat:@"购置日期： %@",self.infoModel.asset.startDateStr],
                               [NSString stringWithFormat:@"入账价值： %.3f",self.infoModel.asset.originalValue],
                               [NSString stringWithFormat:@"折旧年限: %ld",self.infoModel.depreciationYear],
                               [NSString stringWithFormat:@"已提折价： %.3f",self.infoModel.depreciationValue],
                               [NSString stringWithFormat:@"账面净值： %.3f",self.infoModel.leftValue],
                               [NSString stringWithFormat:@"附属设备： %@",self.infoModel.affiliates],
                               [NSString stringWithFormat:@"随机资料： %@",self.infoModel.docu],
                               [NSString stringWithFormat:@"调拨前用途： %@",self.infoModel.beforeAllot],
                               [NSString stringWithFormat:@"调出部门： %@",self.infoModel.outDepartName],
                               [NSString stringWithFormat:@"调出存放地点： %@",self.infoModel.outPlace],
                               [NSString stringWithFormat:@"调出责任人： %@",self.infoModel.outDepartOwnerName],
                               [NSString stringWithFormat:@"调拨后用途： %@",self.infoModel.afterAllot],
                               [NSString stringWithFormat:@"调入部门： %@",self.infoModel.inDepartName],
                               [NSString stringWithFormat:@"调入存放地点： %@",self.infoModel.inPlace],
                               [NSString stringWithFormat:@"调入责任人： %@",self.infoModel.inDepartOwnerName],
                               nil];
    itemModel2.showItemArray = showItemArray2;
    [submitApplyModel.showDataArray addObject:itemModel2];
    [self.showArray addObject:submitApplyModel];
    
    
    XNTableShowDataModel *approveModel = [[XNTableShowDataModel alloc] init];
    approveModel.showType = XNShowType_Approve;
    approveModel.title = @"审批流程";
    [self.approveArray enumerateObjectsUsingBlock:^(XNBillApproveModel *obj, NSUInteger idx, BOOL * _Nonnull stop) {
        XNShowItemModel *temp = [[XNShowItemModel alloc] init];
        temp.showItemArray = [NSArray arrayWithObject:obj];
        [approveModel.showDataArray addObject:temp];
    }];
    [self.showArray addObject:approveModel];
    
}

@end
